package com.t4.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.t4.auth.mapper.SysUserMapper;
import com.t4.auth.service.SysUserService;
import com.t4.model.system.SysUser;
import com.t4.security.custom.LoginUserInfoHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Transactional // 该方法包括先查询后修改
    @Override
    public boolean updateStatus(Long id, Integer status) {
        SysUser sysUser = this.getById(id);
        boolean is_success;
        if(status.intValue() == 1 || status.intValue() == 0){
            sysUser.setStatus(status);
            is_success = this.updateById(sysUser);
        }else{
            is_success = false;
        }
        return is_success;
    }

    @Override
    public SysUser getUserByUsername(String username) {
        SysUser sysUser = baseMapper.selectOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, username));
        return sysUser;
    }

    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = baseMapper.selectById(LoginUserInfoHelper.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getName());
        map.put("phone", sysUser.getPhone());
        return map;
    }
}
